From 582e9d94a856f6813b9820d569a4a70053966346 Mon Sep 17 00:00:00 2001 From: Sondre Lefsaker Date: Sat, 2 May 2015 16:30:16 +0200 Subject: [PATCH] Remove the arguments from `BuildConfig` and append them to the profile that's being compiled instead. - An error will be returned if the length of `targets` is not 1 - The profile of `targets` gets cloned in order to append the extra arguments. - The new test verifies that the build fails due to both `lib` and `main` being compiled --- src/cargo/ops/cargo_compile.rs | 18 +++++++++++++++++- src/cargo/ops/cargo_rustc/mod.rs | 4 +--- tests/test_cargo_rustc.rs | 22 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 38a71dc76..0b7988c39 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -167,12 +167,28 @@ pub fn compile_pkg(package: &Package, options: &CompileOptions) let to_build = packages.iter().find(|p| p.package_id() == pkgid).unwrap(); let targets = try!(generate_targets(to_build, mode, filter, release)); + let target_with_args = match target_rustc_args { + &Some(args) => { + if targets.len() > 1 { + return Err(human("extra arguments to `rustc` can only be \ + invoked for one target")) + } + let (target, profile) = targets[0]; + let mut profile = profile.clone(); + profile.rustc_args = Some(args.to_vec()); + Some((target, profile)) + }, + &None => None, + }; + + let targets = target_with_args.as_ref().map(|&(t, ref p)| vec!((t, p))) + .unwrap_or(targets); + let ret = { let _p = profile::start("compiling"); let mut build_config = try!(scrape_build_config(config, jobs, target)); build_config.exec_engine = exec_engine.clone(); build_config.release = release; - build_config.target_rustc_args = target_rustc_args.map(|a| a.to_vec()); if let CompileMode::Doc { deps } = mode { build_config.doc_all = deps; } diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index a4a5bb1fc..839ec31f7 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -43,7 +43,6 @@ pub struct BuildConfig { pub exec_engine: Option>>, pub release: bool, pub doc_all: bool, - pub target_rustc_args: Option>, } #[derive(Clone, Default)] @@ -624,7 +623,6 @@ fn build_base_args(cx: &Context, opt_level, lto, codegen_units, ref rustc_args, debuginfo, debug_assertions, rpath, test, doc: _doc, } = *profile; - let _ = rustc_args; // Move to cwd so the root_path() passed below is actually correct cmd.cwd(cx.config.cwd()); @@ -666,7 +664,7 @@ fn build_base_args(cx: &Context, cmd.arg("-g"); } - if let Some(ref args) = cx.build_config.target_rustc_args { + if let &Some(ref args) = rustc_args { cmd.args(args); } diff --git a/tests/test_cargo_rustc.rs b/tests/test_cargo_rustc.rs index 87fd961a5..f370986a6 100644 --- a/tests/test_cargo_rustc.rs +++ b/tests/test_cargo_rustc.rs @@ -91,3 +91,25 @@ test!(build_main_and_allow_unstable_options { .with_stdout(verbose_output_for_target_with_args(false, &p, "-Z unstable-options"))); }); + +test!(fails_when_trying_to_build_main_and_lib_with_args { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + + name = "foo" + version = "0.0.1" + authors = ["wycats@example.com"] + "#) + .file("src/main.rs", r#" + fn main() {} + "#) + .file("src/lib.rs", r#" "#); + + + assert_that(p.cargo_process("rustc").arg("-v") + .arg("--").arg("-Z").arg("unstable-options"), + execs() + .with_status(101) + .with_stderr("extra arguments to `rustc` can only be invoked for one target")); +}); -- 2.30.2